<?php

namespace app\console\model;

use think\Model;
use think\Request;

class AuthRule extends Model
{
    // 模型事件
    protected static function init()
    {
        // 写入前
        self::event('before_write', function ($data) {

             $data->parentStr = parentStr(Request::instance()->param('parenTid'), 'auth_rule', Request::instance()->param('id'), Request::instance()->param('repId'));
            $data->count = ruleCount(Request::instance()->param('parenTid'), 'auth_rule', Request::instance()->param('id'));
            $data->update_time = date('Y-m-d', time());

        });
    }


    // 删除方法
    public static function amputate($id)
    {

        if (self::where('parentStr', 'like', '%,' . $id . ',%')->whereOr('id', $id)->delete()) {
            return true;
        }

        return false;

    }

    // 全部删除方法
    public static function amputateAll($id)
    {

        foreach ($id as $value) {
            self::where('parentStr', 'like', '%,' . $value . ',%')->whereOr('id', $value)->delete();
        }

        return true;

    }
}